﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using UBCon.Core.Enums;
using UBCon.Resources;
using UBConDemo.Models;

namespace UBConDemo.ViewModels
{
    /// <summary>
    /// View model for RibbonWindowView view.
    /// </summary>
    public class RibbonWindowViewModel : ViewModelBase
    {
        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------
        #region Constructors

        /// <summary>
        /// Initializes a new instance of UBConDemo.ViewModels.RibbonWindowViewModel class.
        /// </summary>
        public RibbonWindowViewModel()
            : base()
        {

            InitializeCollections();
        }
        
        #endregion

        //------------------------------------------------------
        //
        //  Properties
        //
        //------------------------------------------------------
        #region Properties

        public RibbonBarMode RibbonBarMode
        {
            get
            {
                return _ribbonBarMode;
            }
            set
            {
                if (_ribbonBarMode != value)
                {
                    OptionsItemModel model = (from mode in OptionsButtonSource where mode.Mode == value select mode).First();
                    if (model != SelectedOptionItem)
                    {
                        SelectedOptionItem = model;
                    }
                    _ribbonBarMode = value;
                    OnPropertyChanged("RibbonBarMode");
                }

            }
        }

        public ObservableCollection<OptionsItemModel> OptionsButtonSource
        {
            get
            {
                if (optionsButtonSource == null)
                {
                    optionsButtonSource = new ObservableCollection<OptionsItemModel>();
                }
                return optionsButtonSource;
            }
            set
            {
                optionsButtonSource = value;
            }
        }

        public OptionsItemModel SelectedOptionItem
        {
            get
            {
                return _selectedOptionItem;
            }
            set
            {
                //bool _shouldRaiseEvent = !ReferenceEquals(_selectedOptionItem, value);
                if (!ReferenceEquals(_selectedOptionItem, value))
                {
                    _selectedOptionItem = value;
                    OnPropertyChanged("SelectedOptionItem");
                }

                //if (_shouldRaiseEvent) 
                //{
                //    OnPropertyChanged("SelectedOptionItem");
                //}
                if (_selectedOptionItem != null)
                {
                    if (RibbonBarMode != _selectedOptionItem.Mode)
                    {
                        RibbonBarMode = _selectedOptionItem.Mode;
                    }
                }
            }
        }

        public ObservableCollection<ContentModel> GalleryItemsSource
        {
            get
            {
                return galleryItemsSource;
            }
            set
            {
                galleryItemsSource = value;
                OnPropertyChanged("GalleryItemsSource");
            }
        }

        public ObservableCollection<ContentModel> GalleryBullets
        {
            get
            {
                return galleryBullets;
            }
            set
            {
                galleryBullets = value;
                OnPropertyChanged("GalleryBullets");
            }
        }

        public ObservableCollection<ContentModel> GalleryNumbers
        {
            get { return galleryNumbers; }
            set
            {
                galleryNumbers = value;
                OnPropertyChanged("GalleryNumbers");
            }
        }

        public ObservableCollection<ContentModel> UnderlineItems
        {
            get
            {
                return underlineItems;
            }
            set
            {
                underlineItems = value;
                OnPropertyChanged("UnderlineItems");
            }
        }

        #endregion

        //------------------------------------------------------
        //
        //  Overrides
        //
        //------------------------------------------------------
        #region Overrides
        #endregion

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------
        #region Public Methods
        #endregion

        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
        #region Internal Methods
        #endregion

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        #region Private Methods

        private void InitializeCollections() 
        {
            GalleryItemsSource = new ObservableCollection<ContentModel>();

            for (int i = 0; i < 11; i++)
            {
                if (i >= 1)
                {
                    GalleryItemsSource.Add(
                        new ContentModel()
                        {
                            Image = "pack://application:,,,/UBConDemo;component/images/RibbonBar/Paragraph/paragraph" + i.ToString() + ".png",
                            ImageHeight = 48,
                            ImageWidth = 63
                        });
                }
                else
                {
                    GalleryItemsSource.Add(
                        new ContentModel()
                        {
                            Image = "pack://application:,,,/UBConDemo;component/images/RibbonBar/Paragraph/paragraph" + ".png",
                            ImageHeight = 48,
                            ImageWidth = 63
                        });
                }
            }

            GalleryBullets = new ObservableCollection<ContentModel>();

            for (int i = 0; i < 8; i++)
            {
                if (i >= 1)
                {
                    GalleryBullets.Add(
                        new ContentModel()
                        {
                            Image = "pack://application:,,,/UBConDemo;component/images/RibbonBar/Gallery/GalleryBulletsType" + i.ToString() + ".png",
                            ImageHeight = 32,
                            ImageWidth = 32
                        });
                }
                else
                {
                    GalleryBullets.Add(
                        new ContentModel()
                        {
                            Image = "pack://application:,,,/UBConDemo;component/images/RibbonBar/Gallery/GalleryBulletsNone.png",
                            ImageHeight = 32,
                            ImageWidth = 32
                        });

                }
            }

            GalleryNumbers = new ObservableCollection<ContentModel>();
            for (int i = 0; i < 8; i++)
            {
                if (i >= 1)
                {
                    GalleryNumbers.Add(
                        new ContentModel()
                        {
                            Image = "pack://application:,,,/UBConDemo;component/images/RibbonBar/Gallery/GalleryNumbersType" + i.ToString() + ".png",
                            ImageHeight = 70,
                            ImageWidth = 70
                        });
                }
                else
                {
                    GalleryNumbers.Add(
                        new ContentModel()
                        {
                            Image = "pack://application:,,,/UBConDemo;component/images/RibbonBar/Gallery/GalleryNumbersNone.png",
                            ImageHeight = 70,
                            ImageWidth = 70
                        });
                }
            }

            UnderlineItems = new ObservableCollection<ContentModel>();

            for (int i = 1; i <= 8; i++)
            {
                UnderlineItems.Add(
                    new ContentModel()
                    {
                        Image = "pack://application:,,,/UBConDemo;component/images/RibbonBar/Underline/underline" + i.ToString() + ".png",
                        ImageHeight = 15,
                        ImageWidth = 160
                    });
            }

            InitializeOptionsButtonSource();
        }

        private void InitializeOptionsButtonSource()
        {
            OptionsItemModel model = new OptionsItemModel();
            model.Mode = RibbonBarMode.AutoHideMode;

            model.Title = LocalizationResources.AutomaticallyHideRibbonBarText;
            model.Description = LocalizationResources.AutomaticallyHideRibbonBarDescriptionText;
            model.ImageSource = LocalizationResources.AutomaticallyHideRibbonBarImage;
            if (RibbonBarMode == model.Mode)
            {
                SelectedOptionItem = model;
            }
            OptionsButtonSource.Add(model);

            model = new OptionsItemModel();
            model.Mode = RibbonBarMode.ShowOnlyTabs;
            model.Title = LocalizationResources.ShowTabItemsText;
            model.Description = LocalizationResources.ShowTabItemsTextDescriptionText;
            model.ImageSource = LocalizationResources.ShowTabItemsImage;
            if (RibbonBarMode == model.Mode)
            {
                SelectedOptionItem = model;
            }
            OptionsButtonSource.Add(model);

            model = new OptionsItemModel();
            model.Mode = RibbonBarMode.ShowTabsAndCommands;
            model.Title = LocalizationResources.ShowTabItemsAndCommandText;
            model.Description = LocalizationResources.ShowTabItemsAndCommandDescriptionText;
            model.ImageSource = LocalizationResources.ShowTabItemsAndCommandImage;
            if (RibbonBarMode == model.Mode)
            {
                SelectedOptionItem = model;
            }
            OptionsButtonSource.Add(model);

        }

        #endregion

        //------------------------------------------------------
        //
        //  Event Handlers
        //
        //------------------------------------------------------
        #region Event Handlers
        #endregion

        //------------------------------------------------------
        //
        //  Fields
        //
        //-----------------------------------------------------
        #region Fields

        private OptionsItemModel _selectedOptionItem = null;

        ObservableCollection<OptionsItemModel> optionsButtonSource;

        private ObservableCollection<ContentModel> galleryItemsSource, galleryBullets, galleryNumbers, underlineItems;
        private RibbonBarMode _ribbonBarMode = RibbonBarMode.ShowTabsAndCommands;
        #endregion
    }
}
